﻿using CemsTransfer.BUS;
using CemsTransfer.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

namespace CemsTransfer.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ConfigController : ControllerBase
    {
        public ConfigController()
        {
        }
        /// <summary>
        ///查询串口信息
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("QueryCemsSerialInfo")]
        public ResponseMessage QueryCemsSerialInfo()
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                CemsSerialInfo serialInfo = new CemsSerialInfo();

                serialInfo.BaudRate = Convert.ToInt32(AppSettingsHelper.Configuration["BaudRate"]);
                serialInfo.Parity = Convert.ToInt32(AppSettingsHelper.Configuration["Parity"]);
                serialInfo.DataBits = Convert.ToInt32(AppSettingsHelper.Configuration["DataBits"]);
                serialInfo.StopBits = Convert.ToInt32(AppSettingsHelper.Configuration["StopBits"]);
                serialInfo.PortName = AppSettingsHelper.Configuration["PortName"];

                message.data = serialInfo;
                message.errcode = 0;
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "查询串口信息失败";
            }

            return message;
        }

        /// <summary>
        ///更新串口配置
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("UpdateSerialConfig")]
        public ResponseMessage UpdateSerialConfig(CemsSerialInfo cemsSerialInfo)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {

                string contentPath = AppDomain.CurrentDomain.BaseDirectory;   //项目根目录
                var filePath = contentPath + "appsettings.json";
                JObject jsonObject;
                using (StreamReader file = new StreamReader(filePath))
                using (JsonTextReader reader = new JsonTextReader(file))
                {
                    jsonObject = (JObject)JToken.ReadFrom(reader);
                    jsonObject["BaudRate"] = cemsSerialInfo.BaudRate;
                    jsonObject["Parity"] = cemsSerialInfo.Parity;
                    jsonObject["DataBits"] = cemsSerialInfo.DataBits;
                    jsonObject["StopBits"] = cemsSerialInfo.StopBits;
                    jsonObject["PortName"] = cemsSerialInfo.PortName;
                }

                using (var writer = new StreamWriter(filePath))
                using (JsonTextWriter jsonwriter = new JsonTextWriter(writer))
                {
                    jsonObject.WriteTo(jsonwriter);
                }
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "更新异常";
            }

            return message;
        }

        /// <summary>
        ///查询平台配置信息
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("QueryPlatformInfo")]
        public ResponseMessage QueryPlatformInfo()
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                PlatformInfo platformInfo = new PlatformInfo();
                platformInfo.ServerIP = AppSettingsHelper.Configuration["ServerIP"];
                platformInfo.ServerPort = Convert.ToInt32(AppSettingsHelper.Configuration["ServerPort"]);

                message.data = platformInfo;
                message.errcode = 0;
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "查询平台配置信息失败";
            }

            return message;
        }

        /// <summary>
        ///更新平台配置
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("UpdatePlatformInfo")]
        public ResponseMessage UpdatePlatformInfo(PlatformInfo platformInfo)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {

                string contentPath = AppDomain.CurrentDomain.BaseDirectory;   //项目根目录
                var filePath = contentPath + "appsettings.json";
                JObject jsonObject;
                using (StreamReader file = new StreamReader(filePath))
                using (JsonTextReader reader = new JsonTextReader(file))
                {
                    jsonObject = (JObject)JToken.ReadFrom(reader);
                    jsonObject["ServerIP"] = platformInfo.ServerIP;
                    jsonObject["ServerPort"] = platformInfo.ServerPort;
                }

                using (var writer = new StreamWriter(filePath))
                using (JsonTextWriter jsonwriter = new JsonTextWriter(writer))
                {
                    jsonObject.WriteTo(jsonwriter);
                }
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "更新平台配置异常";
            }

            return message;
        }

        /// <summary>
        ///查询设备配置信息
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("QueryDeviceInfo")]
        public ResponseMessage QueryDeviceInfo()
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                DeviceConfigInfo deviceConfigInfo = new DeviceConfigInfo();
                deviceConfigInfo.DeciceIds = AppSettingsHelper.Configuration["DeciceIds"];
                deviceConfigInfo.Interval = Convert.ToInt32(AppSettingsHelper.Configuration["Interval"]);

                message.data = deviceConfigInfo;
                message.errcode = 0;
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "查询平台配置信息失败";
            }

            return message;
        }

        /// <summary>
        ///更新设备配置
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("UpdateDeviceInfo")]
        public ResponseMessage UpdateDeviceInfo(DeviceConfigInfo deviceConfigInfo)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {

                string contentPath = AppDomain.CurrentDomain.BaseDirectory;   //项目根目录
                var filePath = contentPath + "appsettings.json";
                JObject jsonObject;
                using (StreamReader file = new StreamReader(filePath))
                using (JsonTextReader reader = new JsonTextReader(file))
                {
                    jsonObject = (JObject)JToken.ReadFrom(reader);
                    jsonObject["DeciceIds"] = deviceConfigInfo.DeciceIds;
                    jsonObject["Interval"] = deviceConfigInfo.Interval;
                }

                using (var writer = new StreamWriter(filePath))
                using (JsonTextWriter jsonwriter = new JsonTextWriter(writer))
                {
                    jsonObject.WriteTo(jsonwriter);
                }
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "更新设备配置异常";
            }

            return message;
        }
    }
}
